package com.woniuxy.lab.woniu_lab_boss.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.lab.woniu_lab_boss.common.Result;
import com.woniuxy.lab.woniu_lab_boss.controller.form.AddPortForm;
import com.woniuxy.lab.woniu_lab_boss.service.PortService;
import com.woniuxy.lab.woniu_lab_boss.service.dto.PortDto;
import com.woniuxy.lab.woniu_lab_boss.util.EnumValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 端口表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/port")
@Api(tags = "[H]端口控制相关接口接口")
@Slf4j
@Validated
public class PortController {
    @Resource
    private PortService portService;

    @ApiOperation("94 查看所有port")
    @GetMapping("/queryAllPort")
    public Result<IPage<PortDto>> queryAllPort(@ApiParam(value = "第几页") @Min(value = 1)  Integer page, @ApiParam(value = "每页条数") @Min(value = 1) @Max(value = 10,message = "每页展示数不能大于10") Integer size) {
        log.info("查看所有端口{}{}", page, size);
        IPage<PortDto> all = portService.findAll(page, size);
        return Result.success(200, "查询所有端口成功", all);
    }

    @ApiOperation("89 条件查询port")
    @GetMapping("/queryPort")
    public Result<IPage<PortDto>> queryPort(@ApiParam(value = "第几页") @Min(value = 1,message="页码必须大于1") Integer page,
                                            @ApiParam(value = "每页条数") @Min(value = 1,message="每页展示数必须大于1") @Max(value = 10,message = "每页展示数不能大于10") Integer size,
                                            @ApiParam(value = "关键词 STATIC/DYNAMIC")  String condition) {
        log.info("条件查询端口{}{}{}", page, size, condition);
        IPage<PortDto> all = portService.queryByCondition(page, size, condition);
        return Result.success(200, "条件查询端口成功", all);
    }

    @ApiOperation("90 新增port")
    @PostMapping("/addPort")
    public Result<Object> addPort(@RequestBody @Valid AddPortForm addPortForm) {
        log.info("查看所有端口{}", addPortForm);
        portService.addPort(addPortForm);
        return Result.success(200, "新增端口成功", null);
    }

    @ApiOperation("91 修改port")
    @PutMapping("/modifyPort")
    public Result<Object> modifyPort(@ApiParam(value = "portId") @Min(value = 1) @RequestParam("portId") Integer portId,
                                     @ApiParam(value = "port") @Min(value = 1) @RequestParam("port") String port,
                                     @ApiParam(value = "类型 STATIC/DYNAMIC") @EnumValue(strValues = {"STATIC","DYNAMIC"},message = "类型只能是STATIC/DYNAMIC") @RequestParam("type") String type) {
        log.info("修改{}{}{}端口", portId, port, type);
        portService.modifyPort(portId, port, type);
        return Result.success(200, "修改端口成功", null);
    }

    @ApiOperation("92 移除port")
    @DeleteMapping("/delPort/{portId}")
    public Result<Object> delPort(@PathVariable("portId") Integer portId) {
        log.info("根据id{}删除端口", portId);
        portService.removeById(portId);
        return Result.success(200, "删除端口成功", null);
    }

}

